#
#    Various physic functions for spites
#

from spites_config import *
from math import sqrt
from random import randrange
import spitesutil as S


def distance(A, B):
    return sqrt(sum(map(lambda P: (P[0] - P[1])**2,  zip(A, B))))

def abs_speed(speed):
    return distance(speed, [0, 0])

def sgn(x):
    if x == 0: return 0
    else : return x/abs(x)

# func was not a good function name
def bad_block_acceleration(dist_to_player):
    '''Given the distance between player and
    bad block, may accelerate the bad block
    towards the player'''

    if randrange(100) < S.BAD_BLOCK_ACC_PROB:
        if abs(dist_to_player) > 200: # 
            return S.BAD_BLOCK_ACCELERATION*sgn(dist_to_player)
        else: return sgn(dist_to_player)
    return 0

def block_deceleration(x):
    '''Deceleration due to friction.
    Takes a speed argument x and 
    potentially decreases it'''
    return float(x - sgn(x)*abs(x)/S.DEC_RATE)
    
def block_move_away_acceleration(dist_to_player):
    if abs(dist_to_player) < S.MOVE_AWAY_DIST:
        return S.FOOD_BLOCK_ACC*sgn(dist_to_player)
    return 0
